
/*
===============================================================================
                    EntitySpaces Studio by EntitySpaces, LLC
             Persistence Layer and Business Objects for Microsoft .NET
             EntitySpaces(TM) is a legal trademark of EntitySpaces, LLC
                          http://www.entityspaces.net
===============================================================================
EntitySpaces Version : 2012.1.0930.0
EntitySpaces Driver  : SQL
Date Generated       : 2016/3/21 1:39:13
===============================================================================
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using EntitySpaces.Core;
using EntitySpaces.Interfaces;
using EntitySpaces.DynamicQuery;



namespace yncTech.BusinessObjects
{
	/// <summary>
	/// Encapsulates the 'T_S_Role' table
	/// </summary>

    [DebuggerDisplay("Data = {Debug}")]
	[Serializable]
	[DataContract]
	[KnownType(typeof(TSRole))]	
	[XmlType("TSRole")]
	public partial class TSRole : esTSRole
	{	
		[DebuggerBrowsable(DebuggerBrowsableState.RootHidden | DebuggerBrowsableState.Never)]
		protected override esEntityDebuggerView[] Debug
		{
			get { return base.Debug; }
		}

		override public esEntity CreateInstance()
		{
			return new TSRole();
		}
		
		#region Static Quick Access Methods
		static public void Delete(System.Decimal roleID)
		{
			var obj = new TSRole();
			obj.RoleID = roleID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save();
		}

	    static public void Delete(System.Decimal roleID, esSqlAccessType sqlAccessType)
		{
			var obj = new TSRole();
			obj.RoleID = roleID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save(sqlAccessType);
		}
		#endregion

		
					
		
	
	}



    [DebuggerDisplay("Count = {Count}")]
	[Serializable]
	[CollectionDataContract]
	[XmlType("TSRoleCollection")]
	public partial class TSRoleCollection : esTSRoleCollection, IEnumerable<TSRole>
	{
		public TSRole FindByPrimaryKey(System.Decimal roleID)
		{
			return this.SingleOrDefault(e => e.RoleID == roleID);
		}

		
		
		#region WCF Service Class
		
		[DataContract]
		[KnownType(typeof(TSRole))]
		public class TSRoleCollectionWCFPacket : esCollectionWCFPacket<TSRoleCollection>
		{
			public static implicit operator TSRoleCollection(TSRoleCollectionWCFPacket packet)
			{
				return packet.Collection;
			}

			public static implicit operator TSRoleCollectionWCFPacket(TSRoleCollection collection)
			{
				return new TSRoleCollectionWCFPacket() { Collection = collection };
			}
		}
		
		#endregion
		
				
	}



    [DebuggerDisplay("Query = {Parse()}")]
	[Serializable]	
	public partial class TSRoleQuery : esTSRoleQuery
	{
		public TSRoleQuery():this("TSRoleQuery")
		{
		}
		public TSRoleQuery(string joinAlias)
		{
			this.es.JoinAlias = joinAlias;
		}	

		override protected string GetQueryName()
		{
			return "TSRoleQuery";
		}
		
					
	
		#region Explicit Casts
		
		public static explicit operator string(TSRoleQuery query)
		{
			return TSRoleQuery.SerializeHelper.ToXml(query);
		}

		public static explicit operator TSRoleQuery(string query)
		{
			return (TSRoleQuery)TSRoleQuery.SerializeHelper.FromXml(query, typeof(TSRoleQuery));
		}
		
		#endregion		
	}

	[DataContract]
	[Serializable]
	abstract public partial class esTSRole : esEntity
	{
		public esTSRole()
		{

		}
		
		#region LoadByPrimaryKey
		public virtual bool LoadByPrimaryKey(System.Decimal roleID)
		{
			if(this.es.Connection.SqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(roleID);
			else
				return LoadByPrimaryKeyStoredProcedure(roleID);
		}

		public virtual bool LoadByPrimaryKey(esSqlAccessType sqlAccessType, System.Decimal roleID)
		{
			if (sqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(roleID);
			else
				return LoadByPrimaryKeyStoredProcedure(roleID);
		}

		private bool LoadByPrimaryKeyDynamic(System.Decimal roleID)
		{
			TSRoleQuery query = new TSRoleQuery();
			query.Where(query.RoleID == roleID);
			return this.Load(query);
		}

		private bool LoadByPrimaryKeyStoredProcedure(System.Decimal roleID)
		{
			esParameters parms = new esParameters();
			parms.Add("RoleID", roleID);
			return this.Load(esQueryType.StoredProcedure, this.es.spLoadByPrimaryKey, parms);
		}
		#endregion
		
		#region Properties
		
		
		
		/// <summary>
		/// Maps to T_S_Role.RoleID
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Decimal? RoleID
		{
			get
			{
				return base.GetSystemDecimal(TSRoleMetadata.ColumnNames.RoleID);
			}
			
			set
			{
				if(base.SetSystemDecimal(TSRoleMetadata.ColumnNames.RoleID, value))
				{
					OnPropertyChanged(TSRoleMetadata.PropertyNames.RoleID);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Role.RoleName
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String RoleName
		{
			get
			{
				return base.GetSystemString(TSRoleMetadata.ColumnNames.RoleName);
			}
			
			set
			{
				if(base.SetSystemString(TSRoleMetadata.ColumnNames.RoleName, value))
				{
					OnPropertyChanged(TSRoleMetadata.PropertyNames.RoleName);
				}
			}
		}		
		
		/// <summary>
		/// 1:Active    0:InActive
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Boolean? IsActive
		{
			get
			{
				return base.GetSystemBoolean(TSRoleMetadata.ColumnNames.IsActive);
			}
			
			set
			{
				if(base.SetSystemBoolean(TSRoleMetadata.ColumnNames.IsActive, value))
				{
					OnPropertyChanged(TSRoleMetadata.PropertyNames.IsActive);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Role.CreatedBy
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String CreatedBy
		{
			get
			{
				return base.GetSystemString(TSRoleMetadata.ColumnNames.CreatedBy);
			}
			
			set
			{
				if(base.SetSystemString(TSRoleMetadata.ColumnNames.CreatedBy, value))
				{
					OnPropertyChanged(TSRoleMetadata.PropertyNames.CreatedBy);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Role.CreatedTime
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? CreatedTime
		{
			get
			{
				return base.GetSystemDateTime(TSRoleMetadata.ColumnNames.CreatedTime);
			}
			
			set
			{
				if(base.SetSystemDateTime(TSRoleMetadata.ColumnNames.CreatedTime, value))
				{
					OnPropertyChanged(TSRoleMetadata.PropertyNames.CreatedTime);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Role.LastUpdatedBy
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.String LastUpdatedBy
		{
			get
			{
				return base.GetSystemString(TSRoleMetadata.ColumnNames.LastUpdatedBy);
			}
			
			set
			{
				if(base.SetSystemString(TSRoleMetadata.ColumnNames.LastUpdatedBy, value))
				{
					OnPropertyChanged(TSRoleMetadata.PropertyNames.LastUpdatedBy);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_Role.LastUpdatedTime
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.DateTime? LastUpdatedTime
		{
			get
			{
				return base.GetSystemDateTime(TSRoleMetadata.ColumnNames.LastUpdatedTime);
			}
			
			set
			{
				if(base.SetSystemDateTime(TSRoleMetadata.ColumnNames.LastUpdatedTime, value))
				{
					OnPropertyChanged(TSRoleMetadata.PropertyNames.LastUpdatedTime);
				}
			}
		}		
		
		#endregion	

		#region .str() Properties
		
		public override void SetProperties(IDictionary values)
		{
			foreach (string propertyName in values.Keys)
			{
				this.SetProperty(propertyName, values[propertyName]);
			}
		}
		
		public override void SetProperty(string name, object value)
		{
			esColumnMetadata col = this.Meta.Columns.FindByPropertyName(name);
			if (col != null)
			{
				if(value == null || value is System.String)
				{				
					// Use the strongly typed property
					switch (name)
					{							
						case "RoleID": this.str().RoleID = (string)value; break;							
						case "RoleName": this.str().RoleName = (string)value; break;							
						case "IsActive": this.str().IsActive = (string)value; break;							
						case "CreatedBy": this.str().CreatedBy = (string)value; break;							
						case "CreatedTime": this.str().CreatedTime = (string)value; break;							
						case "LastUpdatedBy": this.str().LastUpdatedBy = (string)value; break;							
						case "LastUpdatedTime": this.str().LastUpdatedTime = (string)value; break;
					}
				}
				else
				{
					switch (name)
					{	
						case "RoleID":
						
							if (value == null || value is System.Decimal)
								this.RoleID = (System.Decimal?)value;
								OnPropertyChanged(TSRoleMetadata.PropertyNames.RoleID);
							break;
						
						case "IsActive":
						
							if (value == null || value is System.Boolean)
								this.IsActive = (System.Boolean?)value;
								OnPropertyChanged(TSRoleMetadata.PropertyNames.IsActive);
							break;
						
						case "CreatedTime":
						
							if (value == null || value is System.DateTime)
								this.CreatedTime = (System.DateTime?)value;
								OnPropertyChanged(TSRoleMetadata.PropertyNames.CreatedTime);
							break;
						
						case "LastUpdatedTime":
						
							if (value == null || value is System.DateTime)
								this.LastUpdatedTime = (System.DateTime?)value;
								OnPropertyChanged(TSRoleMetadata.PropertyNames.LastUpdatedTime);
							break;
					

						default:
							break;
					}
				}
			}
            else if (this.ContainsColumn(name))
            {
                this.SetColumn(name, value);
            }
			else
			{
				throw new Exception("SetProperty Error: '" + name + "' not found");
			}
		}		

		public esStrings str()
		{
			if (esstrings == null)
			{
				esstrings = new esStrings(this);
			}
			return esstrings;
		}

		sealed public class esStrings
		{
			public esStrings(esTSRole entity)
			{
				this.entity = entity;
			}
			
	
			public System.String RoleID
			{
				get
				{
					System.Decimal? data = entity.RoleID;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.RoleID = null;
					else entity.RoleID = Convert.ToDecimal(value);
				}
			}
				
			public System.String RoleName
			{
				get
				{
					System.String data = entity.RoleName;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.RoleName = null;
					else entity.RoleName = Convert.ToString(value);
				}
			}
				
			public System.String IsActive
			{
				get
				{
					System.Boolean? data = entity.IsActive;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.IsActive = null;
					else entity.IsActive = Convert.ToBoolean(value);
				}
			}
				
			public System.String CreatedBy
			{
				get
				{
					System.String data = entity.CreatedBy;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedBy = null;
					else entity.CreatedBy = Convert.ToString(value);
				}
			}
				
			public System.String CreatedTime
			{
				get
				{
					System.DateTime? data = entity.CreatedTime;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.CreatedTime = null;
					else entity.CreatedTime = Convert.ToDateTime(value);
				}
			}
				
			public System.String LastUpdatedBy
			{
				get
				{
					System.String data = entity.LastUpdatedBy;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.LastUpdatedBy = null;
					else entity.LastUpdatedBy = Convert.ToString(value);
				}
			}
				
			public System.String LastUpdatedTime
			{
				get
				{
					System.DateTime? data = entity.LastUpdatedTime;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.LastUpdatedTime = null;
					else entity.LastUpdatedTime = Convert.ToDateTime(value);
				}
			}
			

			private esTSRole entity;
		}
		
		[NonSerialized]
		private esStrings esstrings;		
		
		#endregion
		
		#region Housekeeping methods

		override protected IMetadata Meta
		{
			get
			{
				return TSRoleMetadata.Meta();
			}
		}

		#endregion		
		
		#region Query Logic

		public TSRoleQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSRoleQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSRoleQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return this.Query.Load();
		}
		
		protected void InitQuery(TSRoleQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntity)this).Connection;
			}			
		}

		#endregion
		
        [IgnoreDataMember]
		private TSRoleQuery query;		
	}



	[Serializable]
	abstract public partial class esTSRoleCollection : esEntityCollection<TSRole>
	{
		#region Housekeeping methods
		override protected IMetadata Meta
		{
			get
			{
				return TSRoleMetadata.Meta();
			}
		}

		protected override string GetCollectionName()
		{
			return "TSRoleCollection";
		}

		#endregion		
		
		#region Query Logic

	#if (!WindowsCE)
		[BrowsableAttribute(false)]
	#endif
		public TSRoleQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSRoleQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSRoleQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return Query.Load();
		}

		override protected esDynamicQuery GetDynamicQuery()
		{
			if (this.query == null)
			{
				this.query = new TSRoleQuery();
				this.InitQuery(query);
			}
			return this.query;
		}

		protected void InitQuery(TSRoleQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntityCollection)this).Connection;
			}			
		}

		protected override void HookupQuery(esDynamicQuery query)
		{
			this.InitQuery((TSRoleQuery)query);
		}

		#endregion
		
		private TSRoleQuery query;
	}



	[Serializable]
	abstract public partial class esTSRoleQuery : esDynamicQuery
	{
		override protected IMetadata Meta
		{
			get
			{
				return TSRoleMetadata.Meta();
			}
		}	
		
		#region QueryItemFromName
		
        protected override esQueryItem QueryItemFromName(string name)
        {
            switch (name)
            {
				case "RoleID": return this.RoleID;
				case "RoleName": return this.RoleName;
				case "IsActive": return this.IsActive;
				case "CreatedBy": return this.CreatedBy;
				case "CreatedTime": return this.CreatedTime;
				case "LastUpdatedBy": return this.LastUpdatedBy;
				case "LastUpdatedTime": return this.LastUpdatedTime;

                default: return null;
            }
        }		
		
		#endregion
		
		#region esQueryItems

		public esQueryItem RoleID
		{
			get { return new esQueryItem(this, TSRoleMetadata.ColumnNames.RoleID, esSystemType.Decimal); }
		} 
		
		public esQueryItem RoleName
		{
			get { return new esQueryItem(this, TSRoleMetadata.ColumnNames.RoleName, esSystemType.String); }
		} 
		
		public esQueryItem IsActive
		{
			get { return new esQueryItem(this, TSRoleMetadata.ColumnNames.IsActive, esSystemType.Boolean); }
		} 
		
		public esQueryItem CreatedBy
		{
			get { return new esQueryItem(this, TSRoleMetadata.ColumnNames.CreatedBy, esSystemType.String); }
		} 
		
		public esQueryItem CreatedTime
		{
			get { return new esQueryItem(this, TSRoleMetadata.ColumnNames.CreatedTime, esSystemType.DateTime); }
		} 
		
		public esQueryItem LastUpdatedBy
		{
			get { return new esQueryItem(this, TSRoleMetadata.ColumnNames.LastUpdatedBy, esSystemType.String); }
		} 
		
		public esQueryItem LastUpdatedTime
		{
			get { return new esQueryItem(this, TSRoleMetadata.ColumnNames.LastUpdatedTime, esSystemType.DateTime); }
		} 
		
		#endregion
		
	}


	
	public partial class TSRole : esTSRole
	{

		#region UpToTSFunctionsCollection - Many To Many
		/// <summary>
		/// Many to Many
		/// Foreign Key Name - FK1_T_S_RoleFunctions_RoleID
		/// </summary>

		[XmlIgnore]
		public TSFunctionsCollection UpToTSFunctionsCollection
		{
			get
			{
				if(this._UpToTSFunctionsCollection == null)
				{
					this._UpToTSFunctionsCollection = new TSFunctionsCollection();
					this._UpToTSFunctionsCollection.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("UpToTSFunctionsCollection", this._UpToTSFunctionsCollection);
					if (!this.es.IsLazyLoadDisabled && this.RoleID != null)
					{
						TSFunctionsQuery m = new TSFunctionsQuery("m");
						TSRoleFunctionsQuery j = new TSRoleFunctionsQuery("j");
						m.Select(m);
						m.InnerJoin(j).On(m.FunctionID == j.FunctionID);
                        m.Where(j.RoleID == this.RoleID);

						this._UpToTSFunctionsCollection.Load(m);
					}
				}

				return this._UpToTSFunctionsCollection;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._UpToTSFunctionsCollection != null) 
				{ 
					this.RemovePostSave("UpToTSFunctionsCollection"); 
					this._UpToTSFunctionsCollection = null;
					
				} 
			}  			
		}

		/// <summary>
		/// Many to Many Associate
		/// Foreign Key Name - FK1_T_S_RoleFunctions_RoleID
		/// </summary>
		public void AssociateTSFunctionsCollection(TSFunctions entity)
		{
			if (this._TSRoleFunctionsCollection == null)
			{
				this._TSRoleFunctionsCollection = new TSRoleFunctionsCollection();
				this._TSRoleFunctionsCollection.es.Connection.Name = this.es.Connection.Name;
				this.SetPostSave("TSRoleFunctionsCollection", this._TSRoleFunctionsCollection);
			}

			TSRoleFunctions obj = this._TSRoleFunctionsCollection.AddNew();
			obj.RoleID = this.RoleID;
			obj.FunctionID = entity.FunctionID;
		}

		/// <summary>
		/// Many to Many Dissociate
		/// Foreign Key Name - FK1_T_S_RoleFunctions_RoleID
		/// </summary>
		public void DissociateTSFunctionsCollection(TSFunctions entity)
		{
			if (this._TSRoleFunctionsCollection == null)
			{
				this._TSRoleFunctionsCollection = new TSRoleFunctionsCollection();
				this._TSRoleFunctionsCollection.es.Connection.Name = this.es.Connection.Name;
				this.SetPostSave("TSRoleFunctionsCollection", this._TSRoleFunctionsCollection);
			}

			TSRoleFunctions obj = this._TSRoleFunctionsCollection.AddNew();
			obj.RoleID = this.RoleID;
            obj.FunctionID = entity.FunctionID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
		}

		private TSFunctionsCollection _UpToTSFunctionsCollection;
		private TSRoleFunctionsCollection _TSRoleFunctionsCollection;
		#endregion

		#region TSRoleFunctionsCollectionByRoleID - Zero To Many
		
		static public esPrefetchMap Prefetch_TSRoleFunctionsCollectionByRoleID
		{
			get
			{
				esPrefetchMap map = new esPrefetchMap();
				map.PrefetchDelegate = yncTech.BusinessObjects.TSRole.TSRoleFunctionsCollectionByRoleID_Delegate;
				map.PropertyName = "TSRoleFunctionsCollectionByRoleID";
				map.MyColumnName = "RoleID";
				map.ParentColumnName = "RoleID";
				map.IsMultiPartKey = false;
				return map;
			}
		}		
		
		static private void TSRoleFunctionsCollectionByRoleID_Delegate(esPrefetchParameters data)
		{
			TSRoleQuery parent = new TSRoleQuery(data.NextAlias());

			TSRoleFunctionsQuery me = data.You != null ? data.You as TSRoleFunctionsQuery : new TSRoleFunctionsQuery(data.NextAlias());

			if (data.Root == null)
			{
				data.Root = me;
			}
			
			data.Root.InnerJoin(parent).On(parent.RoleID == me.RoleID);

			data.You = parent;
		}			
		
		/// <summary>
		/// Zero to Many
		/// Foreign Key Name - FK1_T_S_RoleFunctions_RoleID
		/// </summary>

		[XmlIgnore]
		public TSRoleFunctionsCollection TSRoleFunctionsCollectionByRoleID
		{
			get
			{
				if(this._TSRoleFunctionsCollectionByRoleID == null)
				{
					this._TSRoleFunctionsCollectionByRoleID = new TSRoleFunctionsCollection();
					this._TSRoleFunctionsCollectionByRoleID.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("TSRoleFunctionsCollectionByRoleID", this._TSRoleFunctionsCollectionByRoleID);
				
					if (this.RoleID != null)
					{
						if (!this.es.IsLazyLoadDisabled)
						{
							this._TSRoleFunctionsCollectionByRoleID.Query.Where(this._TSRoleFunctionsCollectionByRoleID.Query.RoleID == this.RoleID);
							this._TSRoleFunctionsCollectionByRoleID.Query.Load();
						}

						// Auto-hookup Foreign Keys
						this._TSRoleFunctionsCollectionByRoleID.fks.Add(TSRoleFunctionsMetadata.ColumnNames.RoleID, this.RoleID);
					}
				}

				return this._TSRoleFunctionsCollectionByRoleID;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._TSRoleFunctionsCollectionByRoleID != null) 
				{ 
					this.RemovePostSave("TSRoleFunctionsCollectionByRoleID"); 
					this._TSRoleFunctionsCollectionByRoleID = null;
					
				} 
			} 			
		}
			
		
		private TSRoleFunctionsCollection _TSRoleFunctionsCollectionByRoleID;
		#endregion

		#region UpToTSUsersCollection - Many To Many
		/// <summary>
		/// Many to Many
		/// Foreign Key Name - T_S_UserRole_FK2
		/// </summary>

		[XmlIgnore]
		public TSUsersCollection UpToTSUsersCollection
		{
			get
			{
				if(this._UpToTSUsersCollection == null)
				{
					this._UpToTSUsersCollection = new TSUsersCollection();
					this._UpToTSUsersCollection.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("UpToTSUsersCollection", this._UpToTSUsersCollection);
					if (!this.es.IsLazyLoadDisabled && this.RoleID != null)
					{
						TSUsersQuery m = new TSUsersQuery("m");
						TSUserRolesQuery j = new TSUserRolesQuery("j");
						m.Select(m);
						m.InnerJoin(j).On(m.UserID == j.UserID);
                        m.Where(j.RoleID == this.RoleID);

						this._UpToTSUsersCollection.Load(m);
					}
				}

				return this._UpToTSUsersCollection;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._UpToTSUsersCollection != null) 
				{ 
					this.RemovePostSave("UpToTSUsersCollection"); 
					this._UpToTSUsersCollection = null;
					
				} 
			}  			
		}

		/// <summary>
		/// Many to Many Associate
		/// Foreign Key Name - T_S_UserRole_FK2
		/// </summary>
		public void AssociateTSUsersCollection(TSUsers entity)
		{
			if (this._TSUserRolesCollection == null)
			{
				this._TSUserRolesCollection = new TSUserRolesCollection();
				this._TSUserRolesCollection.es.Connection.Name = this.es.Connection.Name;
				this.SetPostSave("TSUserRolesCollection", this._TSUserRolesCollection);
			}

			TSUserRoles obj = this._TSUserRolesCollection.AddNew();
			obj.RoleID = this.RoleID;
			obj.UserID = entity.UserID;
		}

		/// <summary>
		/// Many to Many Dissociate
		/// Foreign Key Name - T_S_UserRole_FK2
		/// </summary>
		public void DissociateTSUsersCollection(TSUsers entity)
		{
			if (this._TSUserRolesCollection == null)
			{
				this._TSUserRolesCollection = new TSUserRolesCollection();
				this._TSUserRolesCollection.es.Connection.Name = this.es.Connection.Name;
				this.SetPostSave("TSUserRolesCollection", this._TSUserRolesCollection);
			}

			TSUserRoles obj = this._TSUserRolesCollection.AddNew();
			obj.RoleID = this.RoleID;
            obj.UserID = entity.UserID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
		}

		private TSUsersCollection _UpToTSUsersCollection;
		private TSUserRolesCollection _TSUserRolesCollection;
		#endregion

		#region TSUserRolesCollectionByRoleID - Zero To Many
		
		static public esPrefetchMap Prefetch_TSUserRolesCollectionByRoleID
		{
			get
			{
				esPrefetchMap map = new esPrefetchMap();
				map.PrefetchDelegate = yncTech.BusinessObjects.TSRole.TSUserRolesCollectionByRoleID_Delegate;
				map.PropertyName = "TSUserRolesCollectionByRoleID";
				map.MyColumnName = "RoleID";
				map.ParentColumnName = "RoleID";
				map.IsMultiPartKey = false;
				return map;
			}
		}		
		
		static private void TSUserRolesCollectionByRoleID_Delegate(esPrefetchParameters data)
		{
			TSRoleQuery parent = new TSRoleQuery(data.NextAlias());

			TSUserRolesQuery me = data.You != null ? data.You as TSUserRolesQuery : new TSUserRolesQuery(data.NextAlias());

			if (data.Root == null)
			{
				data.Root = me;
			}
			
			data.Root.InnerJoin(parent).On(parent.RoleID == me.RoleID);

			data.You = parent;
		}			
		
		/// <summary>
		/// Zero to Many
		/// Foreign Key Name - T_S_UserRole_FK2
		/// </summary>

		[XmlIgnore]
		public TSUserRolesCollection TSUserRolesCollectionByRoleID
		{
			get
			{
				if(this._TSUserRolesCollectionByRoleID == null)
				{
					this._TSUserRolesCollectionByRoleID = new TSUserRolesCollection();
					this._TSUserRolesCollectionByRoleID.es.Connection.Name = this.es.Connection.Name;
					this.SetPostSave("TSUserRolesCollectionByRoleID", this._TSUserRolesCollectionByRoleID);
				
					if (this.RoleID != null)
					{
						if (!this.es.IsLazyLoadDisabled)
						{
							this._TSUserRolesCollectionByRoleID.Query.Where(this._TSUserRolesCollectionByRoleID.Query.RoleID == this.RoleID);
							this._TSUserRolesCollectionByRoleID.Query.Load();
						}

						// Auto-hookup Foreign Keys
						this._TSUserRolesCollectionByRoleID.fks.Add(TSUserRolesMetadata.ColumnNames.RoleID, this.RoleID);
					}
				}

				return this._TSUserRolesCollectionByRoleID;
			}
			
			set 
			{ 
				if (value != null) throw new Exception("'value' Must be null"); 
			 
				if (this._TSUserRolesCollectionByRoleID != null) 
				{ 
					this.RemovePostSave("TSUserRolesCollectionByRoleID"); 
					this._TSUserRolesCollectionByRoleID = null;
					
				} 
			} 			
		}
			
		
		private TSUserRolesCollection _TSUserRolesCollectionByRoleID;
		#endregion

		
		protected override esEntityCollectionBase CreateCollectionForPrefetch(string name)
		{
			esEntityCollectionBase coll = null;

			switch (name)
			{
				case "TSRoleFunctionsCollectionByRoleID":
					coll = this.TSRoleFunctionsCollectionByRoleID;
					break;
				case "TSUserRolesCollectionByRoleID":
					coll = this.TSUserRolesCollectionByRoleID;
					break;	
			}

			return coll;
		}		
		/// <summary>
		/// Used internally by the entity's hierarchical properties.
		/// </summary>
		protected override List<esPropertyDescriptor> GetHierarchicalProperties()
		{
			List<esPropertyDescriptor> props = new List<esPropertyDescriptor>();
			
			props.Add(new esPropertyDescriptor(this, "TSRoleFunctionsCollectionByRoleID", typeof(TSRoleFunctionsCollection), new TSRoleFunctions()));
			props.Add(new esPropertyDescriptor(this, "TSUserRolesCollectionByRoleID", typeof(TSUserRolesCollection), new TSUserRoles()));
		
			return props;
		}
		
		/// <summary>
		/// Called by ApplyPostSaveKeys 
		/// </summary>
		/// <param name="coll">The collection to enumerate over</param>
		/// <param name="key">"The column name</param>
		/// <param name="value">The column value</param>
		private void Apply(esEntityCollectionBase coll, string key, object value)
		{
			foreach (esEntity obj in coll)
			{
				if (obj.es.IsAdded)
				{
					obj.SetProperty(key, value);
				}
			}
		}
		
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PostSave.
		/// </summary>
		protected override void ApplyPostSaveKeys()
		{
			if(this._TSRoleFunctionsCollection != null)
			{
				Apply(this._TSRoleFunctionsCollection, "RoleID", this.RoleID);
			}
			if(this._TSRoleFunctionsCollectionByRoleID != null)
			{
				Apply(this._TSRoleFunctionsCollectionByRoleID, "RoleID", this.RoleID);
			}
			if(this._TSUserRolesCollection != null)
			{
				Apply(this._TSUserRolesCollection, "RoleID", this.RoleID);
			}
			if(this._TSUserRolesCollectionByRoleID != null)
			{
				Apply(this._TSUserRolesCollectionByRoleID, "RoleID", this.RoleID);
			}
		}
		
	}
	



	[Serializable]
	public partial class TSRoleMetadata : esMetadata, IMetadata
	{
		#region Protected Constructor
		protected TSRoleMetadata()
		{
			m_columns = new esColumnMetadataCollection();
			esColumnMetadata c;

			c = new esColumnMetadata(TSRoleMetadata.ColumnNames.RoleID, 0, typeof(System.Decimal), esSystemType.Decimal);
			c.PropertyName = TSRoleMetadata.PropertyNames.RoleID;
			c.IsInPrimaryKey = true;
			c.IsAutoIncrement = true;
			c.NumericPrecision = 18;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSRoleMetadata.ColumnNames.RoleName, 1, typeof(System.String), esSystemType.String);
			c.PropertyName = TSRoleMetadata.PropertyNames.RoleName;
			c.CharacterMaxLength = 30;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSRoleMetadata.ColumnNames.IsActive, 2, typeof(System.Boolean), esSystemType.Boolean);
			c.PropertyName = TSRoleMetadata.PropertyNames.IsActive;
			c.HasDefault = true;
			c.Default = @"((1))";
			c.Description = "1:Active    0:InActive";
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSRoleMetadata.ColumnNames.CreatedBy, 3, typeof(System.String), esSystemType.String);
			c.PropertyName = TSRoleMetadata.PropertyNames.CreatedBy;
			c.CharacterMaxLength = 40;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSRoleMetadata.ColumnNames.CreatedTime, 4, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = TSRoleMetadata.PropertyNames.CreatedTime;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSRoleMetadata.ColumnNames.LastUpdatedBy, 5, typeof(System.String), esSystemType.String);
			c.PropertyName = TSRoleMetadata.PropertyNames.LastUpdatedBy;
			c.CharacterMaxLength = 40;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSRoleMetadata.ColumnNames.LastUpdatedTime, 6, typeof(System.DateTime), esSystemType.DateTime);
			c.PropertyName = TSRoleMetadata.PropertyNames.LastUpdatedTime;
			c.HasDefault = true;
			c.Default = @"(getdate())";
			m_columns.Add(c);
				
		}
		#endregion	
	
		static public TSRoleMetadata Meta()
		{
			return meta;
		}	
		
		public Guid DataID
		{
			get { return base.m_dataID; }
		}	
		
		public bool MultiProviderMode
		{
			get { return false; }
		}		

		public esColumnMetadataCollection Columns
		{
			get	{ return base.m_columns; }
		}
		
		#region ColumnNames
		public class ColumnNames
		{ 
			 public const string RoleID = "RoleID";
			 public const string RoleName = "RoleName";
			 public const string IsActive = "IsActive";
			 public const string CreatedBy = "CreatedBy";
			 public const string CreatedTime = "CreatedTime";
			 public const string LastUpdatedBy = "LastUpdatedBy";
			 public const string LastUpdatedTime = "LastUpdatedTime";
		}
		#endregion	
		
		#region PropertyNames
		public class PropertyNames
		{ 
			 public const string RoleID = "RoleID";
			 public const string RoleName = "RoleName";
			 public const string IsActive = "IsActive";
			 public const string CreatedBy = "CreatedBy";
			 public const string CreatedTime = "CreatedTime";
			 public const string LastUpdatedBy = "LastUpdatedBy";
			 public const string LastUpdatedTime = "LastUpdatedTime";
		}
		#endregion	

		public esProviderSpecificMetadata GetProviderMetadata(string mapName)
		{
			MapToMeta mapMethod = mapDelegates[mapName];

			if (mapMethod != null)
				return mapMethod(mapName);
			else
				return null;
		}
		
		#region MAP esDefault
		
		static private int RegisterDelegateesDefault()
		{
			// This is only executed once per the life of the application
			lock (typeof(TSRoleMetadata))
			{
				if(TSRoleMetadata.mapDelegates == null)
				{
					TSRoleMetadata.mapDelegates = new Dictionary<string,MapToMeta>();
				}
				
				if (TSRoleMetadata.meta == null)
				{
					TSRoleMetadata.meta = new TSRoleMetadata();
				}
				
				MapToMeta mapMethod = new MapToMeta(meta.esDefault);
				mapDelegates.Add("esDefault", mapMethod);
				mapMethod("esDefault");
			}
			return 0;
		}			

		private esProviderSpecificMetadata esDefault(string mapName)
		{
			if(!m_providerMetadataMaps.ContainsKey(mapName))
			{
				esProviderSpecificMetadata meta = new esProviderSpecificMetadata();			


				meta.AddTypeMap("RoleID", new esTypeMap("numeric", "System.Decimal"));
				meta.AddTypeMap("RoleName", new esTypeMap("varchar", "System.String"));
				meta.AddTypeMap("IsActive", new esTypeMap("bit", "System.Boolean"));
				meta.AddTypeMap("CreatedBy", new esTypeMap("varchar", "System.String"));
				meta.AddTypeMap("CreatedTime", new esTypeMap("datetime", "System.DateTime"));
				meta.AddTypeMap("LastUpdatedBy", new esTypeMap("varchar", "System.String"));
				meta.AddTypeMap("LastUpdatedTime", new esTypeMap("datetime", "System.DateTime"));			
				
				
				
				meta.Source = "T_S_Role";
				meta.Destination = "T_S_Role";
				
				meta.spInsert = "proc_T_S_RoleInsert";				
				meta.spUpdate = "proc_T_S_RoleUpdate";		
				meta.spDelete = "proc_T_S_RoleDelete";
				meta.spLoadAll = "proc_T_S_RoleLoadAll";
				meta.spLoadByPrimaryKey = "proc_T_S_RoleLoadByPrimaryKey";
				
				this.m_providerMetadataMaps["esDefault"] = meta;
			}
			
			return this.m_providerMetadataMaps["esDefault"];
		}

		#endregion

		static private TSRoleMetadata meta;
		static protected Dictionary<string, MapToMeta> mapDelegates;
		static private int _esDefault = RegisterDelegateesDefault();
	}
}
